<?php
$prefix = 'center';
if($_POST['action']=='save')
{
	$data = array(
		'membership_id'=>$_POST['owner'],
		'name'=>$_POST['name'],
		'address'=>$_POST['address'],
		'phone'=>$_POST['tel'],
		'username'=>$_POST['username'],
		'password'=>$_POST['password']
	);	
	if($_POST['id']){
		update("tb_$prefix",$data,"id = '".$_POST['id']."'");
		$id = $_POST['id'];
	}else{
		insert("tb_$prefix",$data);	
		$id = mysql_insert_id();	
	}
	$fileupload=$_FILES['fileupload']['tmp_name'];
	$fileupload_name=$_FILES['fileupload']['name'];
	$fileupload_size=$_FILES['fileupload']['size'];
	$fileupload_type=$_FILES['fileupload']['type'];
	
	if ($fileupload) {
			$array_last=explode(".",$fileupload_name);
			$c=count($array_last)-1; 
			$lastname=strtolower($array_last[$c]) ;
			$photoname=date("YmdHis")."_".$id.".".$lastname;
			$image = new SimpleImage();
			$image->load($fileupload);
			
			if($image->getWidth()>500){
				$image->resizeToWidth(500);
			}
			if($image->getHeight()>500){
				$image->resizeToHeight(500);
			}
			$image->save("../upload/$photoname");
			$data = array(
				"image"=>($photoname),
			);
			update("tb_$prefix",$data,"id='".$id."'");											
			unlink($fileupload);
		}
		echo "<script>window.location='admin.php?page=$prefix'</script>";
}
$sql = "select * from tb_$prefix where id = '".$_GET['id']."'";
$qr = select($sql);
$rs = $qr[0];
if($_GET['id']){
	$text = "แก้ไข";	
}else{
	$text = "เพิ่ม";
}
?>
<div id="tabs">
    <ul>
    	<li><a class="head_tab" style="padding-left:7px;" href="#tabs-1"><span class="ui-icon ui-icon-calendar" style="float:left;"></span><?=$text;?>ศูนย์</a></li>
    </ul>
    <div id="tabs-1">
    	<form action="" method="post" enctype="multipart/form-data" id="content-form">
          <table width="100%" border="0" cellspacing="0" cellpadding="5">
                  <tr>
                    <td width="8%"><strong>ผู้ตั้ง</strong></td>
                    <td width="92%">
                        <select name="owner" id="owner">
                        	<?php
							$sql = "select * from tb_membership where level = 'platinum' and username<>''";
							$qr  = select($sql);
							$total = count($qr);
							$i=0;
							while($i<$total){
								$rs2 = $qr[$i];
								$i++;
							?>
                        	<option value="<?=$rs2['id']?>"><?=$rs2['name']?> (<?=$rs2['username']?>)</option>
                            <?php
							}
							?>
                        </select>
                    </td>
                </tr>
                  <tr>
                    <td><strong>username</strong></td>
                    <td><input type="text" name="username" id="username" value="<?=$rs['username']?>" /></td>
                  </tr>
                  <tr>
                    <td><strong>password</strong></td>
                    <td><input type="password" name="password" id="password" value="<?=$rs['password']?>" /></td>
                  </tr>
                  <tr>
                    <td><strong>ชื่อศูนย์</strong></td>
                    <td><input type="text" name="name" id="name" value="<?=$rs['name']?>" /></td>
                  </tr>
                  <tr>
                    <td><strong>เบอร์โทร</strong></td>
                    <td><input type="text" name="tel" id="tel" value="<?=$rs['phone']?>" /></td>
                  </tr>
                  <tr>
                    <td valign="top"><strong>ที่อยู่</strong></td>
                    <td>
                    <textarea name="address" id="address" cols="30" rows="5"><?=$rs['address']?></textarea></td>
                </tr>
                  <tr>
                    <td><strong>แผนที่</strong></td>
                    <td>
                    <?php
						if($rs['image']<>''){
							echo "<img src=\"../upload/$rs[image]\" width=100/>";
						}
					?>
                    <input type="file" name="fileupload" id="fileupload" />
                    ขนาดรูปภาพที่แนะนำ 139*139 pixel</td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="button" id="button" value="Submit" />
                    <input name="id" type="hidden" id="id" value="<?=$rs['id']?>" />
                    <input name="action" type="hidden" id="action" value="save" /></td>
                  </tr>
          </table>
      </form>
    </div>
</div>            